package com.web.util;

import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author
 * @Description
 * @Date
 */
public class JDBCUtils {
    private static Connection connection;

    public static int update(String sql, Object... args) {
        try (Connection conn = DataSourceUtils.getConnection()) {
            PreparedStatement ps = conn.prepareStatement(sql);
            //设置参数
            for (int i = 0; i < args.length; i++) {
                ps.setObject(i + 1, args[i]);
            }
            int rows = ps.executeUpdate();
            return rows;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    public static <T> List<T> query(Class<T> clazz, String sql, Object... args) {
        try (Connection conn = DataSourceUtils.getConnection()) {
            PreparedStatement ps = conn.prepareStatement(sql);
            //设置参数
            for (int i = 0; i < args.length; i++) {
                ps.setObject(i + 1, args[i]);
            }
            //创建list集合
            List<T> list = new ArrayList<>();
            //执行查询
            ResultSet resultSet = ps.executeQuery();
            //遍历结果集
            while (resultSet.next()) {
                //创建对象
                T obj = clazz.newInstance();
                //获得所有的属性
                Field[] fields = clazz.getDeclaredFields();
                for (Field field : fields) {
                    //通过属性名读取结果集中的值
                    Object value = resultSet.getObject(field.getName());
                    //设置属性访问性
                    field.setAccessible(true);
                    //设置属性的值
                    field.set(obj, value);
                }
                //添加到集合
                list.add(obj);
            }
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;

    }


    public static void close(Connection conn, PreparedStatement pstmt, ResultSet rs) {
        try {
            if (rs != null) {
                rs.close();
            }
            if (pstmt != null) {
                pstmt.close();
            }
            if (conn != null) {
                conn.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static int queryCount(String sql,Object... args) {
        try (Connection conn = DataSourceUtils.getConnection()) {
            PreparedStatement ps = conn.prepareStatement(sql);
            //设置参数
            for (int i = 0; i < args.length; i++) {
                ps.setObject(i + 1, args[i]);
            }
            ResultSet rs = ps.executeQuery();
            if(rs.next()){
                return rs.getInt(1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }


    public static Connection getConnection() {

        return connection;
    }




}
